import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: '完成项目报告', completed: false, createTime: '2024-01-15 10:30' },
      { id: 2, text: '购买生活用品', completed: true, createTime: '2024-01-14 15:20' },
      { id: 3, text: '预约医生检查', completed: false, createTime: '2024-01-13 09:15' }
    ],
    notes: [
      { 
        id: 1, 
        title: '会议记录', 
        content: '今天的团队会议讨论了项目进度，确定了下周的工作重点。\n1. 前端界面优化\n2. 后端接口完善\n3. 测试用例编写', 
        createTime: '2024-01-15 14:30',
        updateTime: '2024-01-15 14:30'
      },
      { 
        id: 2, 
        title: '学习笔记', 
        content: 'Vue.js 组件通信方式：\n- props 父传子\n- $emit 子传父\n- provide/inject 跨层级\n- Vuex 状态管理', 
        createTime: '2024-01-14 16:45',
        updateTime: '2024-01-14 16:45'
      }
    ]
  },
  mutations: {
    ADD_TODO(state, todo) {
      state.todos.unshift(todo)
    },
    UPDATE_TODO(state, { id, completed }) {
      const todo = state.todos.find(item => item.id === id)
      if (todo) {
        todo.completed = completed
      }
    },
    DELETE_TODO(state, id) {
      state.todos = state.todos.filter(item => item.id !== id)
    },
    ADD_NOTE(state, note) {
      state.notes.unshift(note)
    },
    UPDATE_NOTE(state, updatedNote) {
      const index = state.notes.findIndex(note => note.id === updatedNote.id)
      if (index !== -1) {
        state.notes[index] = updatedNote
      }
    },
    DELETE_NOTE(state, id) {
      state.notes = state.notes.filter(note => note.id !== id)
    }
  },
  actions: {
    addTodo({ commit }, todo) {
      commit('ADD_TODO', todo)
    },
    updateTodo({ commit }, payload) {
      commit('UPDATE_TODO', payload)
    },
    deleteTodo({ commit }, id) {
      commit('DELETE_TODO', id)
    },
    addNote({ commit }, note) {
      commit('ADD_NOTE', note)
    },
    updateNote({ commit }, note) {
      commit('UPDATE_NOTE', note)
    },
    deleteNote({ commit }, id) {
      commit('DELETE_NOTE', id)
    }
  },
  getters: {
    completedTodos: state => state.todos.filter(todo => todo.completed),
    pendingTodos: state => state.todos.filter(todo => !todo.completed),
    getNoteById: state => id => state.notes.find(note => note.id === id)
  }
})

export default store